package com.cheer.ba.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.cheer.ba.R;
import com.cheer.ba.constant.IntentCode;
import com.cheer.ba.constant.IntentKey;
import com.cheer.ba.model.CityModel;
import com.cheer.ba.model.ProvinceModel;
import com.cheer.ba.presenter.impl.OilMealPresenter;
import com.cheer.ba.view.base.BaseCommAty;
import com.cheer.ba.view.base.CommonAdapter;
import com.cheer.ba.view.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 地区第二级
 *
 * @author HQ
 *         created at 2017/5/15 0015 13:25
 */
public class CityAreaAty extends BaseCommAty implements AdapterView.OnItemClickListener {

    @BindView(R.id.id_prov_list)
    ListView idCityListView;

    private CommonAdapter<CityModel> mAdapter;
    private List<CityModel> mCitys = new ArrayList<CityModel>();

    private ProvinceModel provinceModel;

    @Override
    public View loadView(LayoutInflater inflater, View childView) {
        return super.loadView(inflater, inflater.inflate(R.layout.aty_my_area, null, false));
    }

    @Override
    public void initPresenter() {
        presenter = new OilMealPresenter();
    }

    @Override
    public void initView() {
        setTitleText(getString(R.string.mine_area));
        provinceModel = (ProvinceModel) getIntent().getExtras().getSerializable(IntentKey.General.KEY_MODEL);

        View headView = LayoutInflater.from(this).inflate(R.layout.layout_area_head1, null);
        ((TextView) headView.findViewById(R.id.id_choose_area_txt)).setText("当前城市：" + provinceModel.mProvinceName);
        idCityListView.addHeaderView(headView);

        mAdapter = new CommonAdapter<CityModel>(this, mCitys, R.layout.layout_area_item) {
            @Override
            public void convert(ViewHolder helper, CityModel item) {
                ((TextView) helper.getView(R.id.id_area_check)).setText(item.isCChecked ? "已选中" : "");
                ((TextView) helper.getView(R.id.id_area_text)).setText(item.mCityName);
            }
        };
        idCityListView.setAdapter(mAdapter);
        idCityListView.setOnItemClickListener(this);
    }

    @Override
    public void initData() {
        List<CityModel> citys = (List<CityModel>) ((OilMealPresenter) presenter).getData(5,false);
        if (null != citys && !citys.isEmpty()) {
            getAreaList(citys);
            mAdapter.notifyDataSetChanged();
        }
    }

    /**
     * 更新被选中数据
     */
    public void getAreaList(List<CityModel> citys) {
        mCitys.clear();
        for (CityModel city : citys) {
            if (TextUtils.equals(provinceModel.mPId, city.mPID)) {
                mCitys.add(city);
            }
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (position != 0) {
            CityModel model = mCitys.get(position - 1);
            Intent intent = new Intent(this, DistrictsAreaAty.class);
            intent.putExtra(IntentKey.General.KEY_DATA, provinceModel.mProvinceName);
            Bundle data = new Bundle();
            data.putSerializable(IntentKey.General.KEY_MODEL, model);
            intent.putExtras(data);
            startActivityForResult(intent, IntentCode.Area.DISTRICT_REQUEST_CODE);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == IntentCode.Area.DISTRICT_REQUEST_CODE) {
            if (resultCode == IntentCode.Area.DISTRICT_RESULT_CODE) {
                if (null != data) {
                    String mSaveArea = data.getStringExtra(IntentKey.General.KEY_DATA);
                    Intent intent = new Intent();
                    intent.putExtra(IntentKey.General.KEY_DATA, mSaveArea);
                    setResult(IntentCode.Area.CITY_RESULT_CODE, intent);
                    finish();
                }
            }
        }
    }
}
